<template>
  <div>
    <el-table :data="newds" border stripe>
      <el-table-column prop="id" label="编号"></el-table-column>
      <el-table-column prop="title" label="名字"></el-table-column>
      <el-table-column prop="content" label="价格"></el-table-column>
      <el-table-column prop="author" label="描述"></el-table-column>
      <el-table-column prop="time" label="发布时间"></el-table-column>
      <el-table-column>
        <template slot-scope="scope">
          <el-button type="danger" @click="deleteById(scope.row.id)">删除</el-button>
          <el-button type="warning" @click="selectById(scope.row.id)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-button type="primary" @click="shownewd=true">添加</el-button>

    <el-dialog :visible.sync="showHero" title="修改" width="50%">
      <span>
        <el-form ref="myForm" :model="newd">
          <el-form-item label="名字" prop="title">
            <el-input type="text" v-model="newd.title"></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="content">
            <el-input type="text" v-model="newd.content"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="author">
            <el-input type="text" v-model="newd.author"></el-input>
          </el-form-item>
<!--           <el-form-item label="发布时间" prop="time">-->
<!--            <el-input type="text" v-model="newd.time"></el-input>-->
<!--          </el-form-item>-->
        </el-form>
      </span>
      <span slot="footer">
        <el-button type="warning" @click="updateById">确定</el-button>
        <el-button type="danger" @click="updateCancel">取消</el-button>
      </span>
    </el-dialog>


    <el-dialog :visible.sync="shownewd" title="添加" width="50%">
      <span>
        <el-form ref="myForm" :model="newd">
          <el-form-item label="名字" prop="title">
            <el-input type="text" v-model="newd.title"></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="content">
            <el-input type="text" v-model="newd.content"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="author">
            <el-input type="text" v-model="newd.author"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer">
        <el-button type="warning" @click="insert">确定</el-button>
        <el-button type="danger" @click="insertCancel">确定</el-button>
      </span>
    </el-dialog>




  </div>
</template>

<script>
export default {
  name: "newd",
  data(){
    return{
      newds:[],
      showHero:false,
      newd:{},
      shownewd:false,
      value1:''
    }
  },
  methods:{
    selectAll(){
      this.axios.get("/newd/selectAll").then((resp)=>{
        this.newds=resp.data
      })
    },
    deleteById(id){
      this.axios.delete("/newd/"+id).then((resp)=>{
        this.selectAll()
        this.$message.success("删除成功")
      }).catch((error)=>{
        this.$message.success("删除失败")
      })
    },
    selectById(id){
      this.axios.get("/newd/"+id).then((resp)=>{
         this.newd=resp.data
        this.showHero=true
      })
    },
    updateCancel(){
      this.showHero=false
      this.newd={}
    },
    updateById(){
     this.axios({
       method:'put',
       url:'/newd/update',
       data:this.newd
     }).then((resp)=>{
       this.showHero=false
       this.newd={},
       this.$message.success("修改成功")
       this.selectAll()
     })
    },
    insert(){
        this.axios({
          method:'post',
          url:'/newd/insert',
          data:this.newd
        }).then((resp)=>{
          this.shownewd=false
          this.newd={}
          this.$message.success("添加成功")
          this.selectAll()
        })
    },
    insertCancel(){
      this.shownewd=false
      this.newd={}
    }
  },
  created() {
    this.selectAll()
  }
}
</script>

<style scoped>

</style>
